from django.shortcuts import render
from preprocessor.drop_duplicates import DropDuplicates
from preprocessor.fillna import FillNa
from BigDataWeb.view import get_algorithm
from BigDataWeb.view import put_algorithm
from BigDataWeb.view import read_data_source


def drop_duplicates(request):
    context = {}
    algorithm = DropDuplicates()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "preprocessor/1.html", context)


def fillna(request):
    context = {}
    algorithm = FillNa()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "preprocessor/1.html", context)


def select_data_source(request):
    context = {}
    algorithm = get_algorithm(request)
    read_data_source(request, algorithm)
    context["algorithm"] = algorithm
    return render(request, "preprocessor/2.html", context)


def understand_data_set(request):
    context = {}
    algorithm = get_algorithm(request)
    algorithm.setInPutFieldName(request.POST.getlist("input_field_names"))
    context["algorithm"] = algorithm
    return render(request, "preprocessor/3.html", context)


def configure_parameters(request):
    context = {}
    algorithm = get_algorithm(request)
    
    if algorithm.algorithm_name == "去重":
        algorithm.keep = request.POST.get("keep")
    
    if algorithm.algorithm_name == "缺失值":
        algorithm.methods = request.POST.getlist("methods")
    
    algorithm.implent()
    algorithm.saveToExcle()
    algorithm.generateNotebookFile()

    context["algorithm"] = algorithm
    return render(request, "preprocessor/5.html", context)

